package schoolsource.gui;

//import java.util.logging.*;
//import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Manages string length.
 */
public class DisplayString {

    // contains the widths of characters for font size 4
    static int charArray[][] = new int[][] {{11,(int)'A'},{10,(int)'B'},{11,(int)'C'},{11,(int)'D'},{9, (int)'E'},{9, (int)'F'},
                                            {11,(int)'G'},{11,(int)'H'},{5, (int)'I'},{6, (int)'J'},{12,(int)'K'},{9, (int)'L'},
                                            {14,(int)'M'},{12,(int)'N'},{12,(int)'O'},{9, (int)'P'},{12,(int)'Q'},{10,(int)'R'},
                                            {9, (int)'S'},{9, (int)'T'},{11,(int)'U'},{11,(int)'V'},{15,(int)'W'},{11,(int)'X'},
                                            {11,(int)'Y'},{9, (int)'Z'},{7, (int)'a'},{8, (int)'b'},{7, (int)'c'},{8, (int)'d'},
                                            {7, (int)'e'},{4, (int)'f'},{7, (int)'g'},{7, (int)'h'},{3, (int)'i'},{4, (int)'j'},
                                            {8, (int)'k'},{3, (int)'l'},{11,(int)'m'},{7, (int)'n'},{8, (int)'o'},{8, (int)'p'},
                                            {8, (int)'q'},{5, (int)'r'},{6, (int)'s'},{4, (int)'t'},{7, (int)'u'},{7, (int)'v'},
                                            {11,(int)'w'},{7, (int)'x'},{7, (int)'y'},{6, (int)'z'},{8,(int)'0'},{8,(int)'1'},
                                            {8,(int)'2'},{8,(int)'3'},{8,(int)'4'},{8,(int)'5'},{8,(int)'6'},{8,(int)'7'},
                                            {8,(int)'8'},{8,(int)'9'},{5, (int)'`'},{9, (int)'~'},{5, (int)'!'},{15,(int)'@'},
                                            {8, (int)'#'},{8, (int)'$'},{13,(int)'%'},{8, (int)'^'},{12,(int)'&'},{8, (int)'*'},
                                            {5, (int)'('},{5, (int)')'},{6, (int)'-'},{8, (int)'_'},{9, (int)'='},{9, (int)'+'},
                                            {5, (int)'['},{7, (int)'{'},{5, (int)']'},{8, (int)'}'},{4, (int)'\\'},{3, (int)'|'},
                                            {4, (int)';'},{3, (int)':'},{3, (int)'\''},{5, (int)'"'},{4, (int)','},{9, (int)'<'},
                                            {4, (int)'.'},{9, (int)'>'},{4, (int)'/'},{7, (int)'?'},{4, (int)' '}};

    static int intArray[] = new int[256];
    // contains widths of characters for font size 4 - indexed by the int value of the character
    static int sizeArray[] = new int[] {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                                        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,  4,  5,  5,  8,  8, 13, 12,  3,  5,  5,
                                         8,  9,  4,  6,  4,  4,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  3,  4,  9,  9,  9,
                                         7, 15, 11, 10, 11, 11,  9,  9, 11, 11,  5,  6, 12,  9, 14, 12, 12,  9, 12, 10,  9,
                                         9, 11, 11, 15, 11, 11,  9,  5,  4,  5,  8,  8,  5,  7,  8,  7,  8,  7,  4,  7,  7,
                                         3,  4,  8,  3, 11,  7,  8,  8,  8,  5,  6,  4,  7,  7, 11,  7,  7,  6,  7,  3,  8,
                                         9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                                        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                                        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                                        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                                        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                                        10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
                                        10, 10, 10, 10};

    /**
     * Tests the functions in this class.
     * @param args No accepted parameters
     */
    public static void main(String[] args) {
        for (int i = 0; i < intArray.length; i++) {
            intArray[i] = 10;
        }

        for (int i = 0; i < charArray.length; i++) {
            intArray[charArray[i][1]] = charArray[i][0];
        }

//        System.out.println("new int[] = {");

        for (int i = 0; i < intArray.length; i++) {
//            System.out.print("" + intArray[i] + ", ");
            if (i % 10 == 0) {
//                System.out.println("");
            }
        }
//        System.out.println("");

//        System.out.println("");

//        System.out.println("!: " + sizeArray['!']);
//        System.out.println("{: " + sizeArray['}']);

        String alphabet = "abcdefghijklmnopqrstuvwxyz";
//        System.out.println(alphabet + " length: " + size(alphabet, 4));
//        System.out.println(alphabet + " that fits in 100: " + getTruncatedString(alphabet, 100, 4));
//        System.out.println(alphabet + " that fits in 99: " + getTruncatedString(alphabet, 99, 4));
//        System.out.println(alphabet + " that fits in 0: " + getTruncatedString(alphabet, 0, 4));
//        System.out.println(alphabet + " that fits in 10000: " + getTruncatedString(alphabet, 10000, 4));
    }

    /**
     * Calculates the size of a string.
     * @param evalString The string to evaluate the length of
     * @param fontSize The font size used to display the string. This is a placeholder for future use.
     * @return The size of the string in the given font size
     */
    public static int size(String evalString, int fontSize) {
        int total = 0;
        char charArray[] = evalString.toCharArray();

        for (int i = 0; i < charArray.length; i++) {
            total += sizeArray[charArray[i]];
        }

        return total;
    }

    /**
     * Truncates a string to no longer than a given length
     * @param input The string to truncate
     * @param availableSize The size available
     * @param font The font used to display. This is not currently used. A font size of 4 is assumed
     * @return The preview string with ellipsis appended if appropriate
     */
    public static String getTruncatedString(String input, int availableSize, int font) {
        int total = 0;
        String returnString = null;
        int last = -1;
        String ellipsis = "...";
availableSize *= 1.40;
        if (size(input, font) > availableSize) {
            total += size(ellipsis, font);
        }
        else {
            return input;
        }

        for (int i = 0; i < input.length() && total < availableSize; i++) {
            total += sizeArray[input.charAt(i)];

            if (total <= availableSize) {
                last = i;
            }
        }
        if (last < 0) {
            return "";
        }
        else {
            return input.substring(0, last + 1) + ellipsis;
        }
    }
}
